package com.yoda.mall.admin.controller;

import com.yoda.common.Status;
import com.yoda.mall.admin.intercepter.ThreadContext;
import com.yoda.common.validator.group.InsertGroup;
import com.yoda.common.validator.group.UpdateGroup;
import com.yoda.mall.admin.auth.JwtUtils;
import com.yoda.mall.admin.service.AdminService;
import com.yoda.mall.model.dto.LoginDto;
import com.yoda.mall.model.entity.ums.Admin;
import com.yoda.mall.model.entity.ums.Role;
import com.yoda.mall.model.vo.ManagerInfoVo;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 12月 15日 19:39
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
@Resource
private AdminService adminService;
    @PostMapping("/login")
    public R login(HttpServletRequest request , @RequestBody LoginDto dto){
        log.info("接收到前端登录请求:{}",dto);
        //避免误触
        if (Objects.isNull(dto)|| StringUtils.isEmpty(dto.getUsername())||StringUtils.isEmpty(dto.getPassword())){
            return R.error("账号或密码为空!", Status.UN_AUTHORIZED.getCode());
        }

        //调用业务层查询数据库,相同用户名用户
       return adminService.login(request,dto);
    }

    @GetMapping("/info")
    public R managerInfo(){
        //获取当前用户信息以及相应权限
        ManagerInfoVo vo = adminService.getManagerInfo();
        return R.success(vo);
    }

    @PostMapping("/logout")
    public R logout(){
        ThreadContext.clear();
        return R.success();
    }

    /**
     * 解析令牌
     *
     * @param token 令牌
     * @return {@link Long}
     */
    @GetMapping("/parseToken/{token}")
    public String parseToken(@PathVariable String token){
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        String username = (String) claims.get("username");
        return id+","+username;
    }

    /**
     * 得到帐户页面
     *
     * @param keyword  关键字
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @return {@link R}
     */
    @GetMapping("/list")
    public R getAccountByPage(String keyword, @RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult<Admin> pageResult = adminService.selectByPage(keyword,pageNum,pageSize);
        return R.success(pageResult);
    }

    /**
     * 注册账户
     *
     * @param admin 管理
     * @return {@link R}
     */
    @PostMapping("/register")
    public R registerAccount(@Validated(InsertGroup.class) @RequestBody Admin admin){
      Admin account =  adminService.registerAccount(admin);
      return R.success(account);
    }

    /**
     * 分配角色
     *
     * @return {@link R}
     */
    @PostMapping("/role/update")
    public R assigningRoles(@RequestParam Long adminId,@RequestParam List<Long>roleIds){
        if (Objects.isNull(roleIds)||roleIds.size()==0){
            return R.error("角色为空",Status.FORBIDDEN.getCode());
        }
        Integer count =adminService.assigningRoles(adminId,roleIds);
        return R.success(count);
    }

    /**
     * 更新账号基础信息
     *
     * @return {@link R}
     */
    @PostMapping("/update")
    public R updateAccount(@Validated(UpdateGroup.class) @RequestBody Admin admin){
        Integer count =adminService.updateAccount(admin);
        return R.success(count);
    }

    /**
     * 更新帐户状态
     *
     * @param id     id
     * @param status 状态
     * @return {@link R}
     */
    @PostMapping("/updateStatus/{id}")
    public R updateAccountStatus(@PathVariable Long id,Integer status){
        Integer count = adminService.updateAccountStatus(id,status);
        return R.success(count);
    }
    /**
     * 删除账号
     *
     * @return {@link R}
     */
    @PostMapping("/delete/{id}")
    public R deleteAccount(@PathVariable Long id){
        Integer count =adminService.deleteAccountById(id);
        return R.success(count);
    }

    /**
     * 得到指定管理员id的对应角色
     *
     * @param adminId 管理员id
     * @return {@link R}
     */
    @GetMapping("/role/{adminId}")
    public R getSpecifyRole(@PathVariable Long adminId){
        List<Role> roles = adminService.getSpecifyRole(adminId);
        return R.success(roles);
    }



}
